﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Bing.Maps;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace BingMap
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            DataContext = new ViewModel();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
    }

    public class ViewModel
    {
        private ObservableCollection<MapShapeLayer> _ShapeLayers;
        private ObservableCollection<MapItem> _items;

        public ViewModel()
        {
            _ShapeLayers = new ObservableCollection<MapShapeLayer>()
            {
                new MapShapeLayer { Visible = true, Shapes = new MapShapeCollection
                    {
                        new MapPolygon { FillColor = Colors.Green, Visible = true, Locations = new LocationCollection
                        {
                               new Location(51.062113, 2.922363), new Location(53.748711, 6.745605), 
                               new Location(51.399206, 2.922363), new Location(50.930738, 6.657715)
                            }
                        }
                    }
                }
            };

            _items = new ObservableCollection<MapItem>()
            {
                new MapItem { Title = "Een item", Latitude = 51.399206, Longitude = 2.922363 }
            };
        }

        public ObservableCollection<MapShapeLayer> ShapeLayers
        {
            get { return _ShapeLayers; }
            set { _ShapeLayers = value; }
        }

        public ObservableCollection<MapItem> Items
        {
            get { return _items; }
            set { _items = value; }
        }
    }

    public class MapItemDataTemplateSelector : DataTemplateSelector
    {
        protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
        {
            return (DataTemplate)Application.Current.Resources["ItemTemplate"];
        }
    }
}
